package it.orma.iridehr.controllers;

import java.util.HashMap;

import it.orma.iridehr.entities.SchedaCandidato;
import it.orma.iridehr.repository.RicercaRepository;
import it.orma.iridehr.service.AlfrescoService;
import it.orma.iridehr.service.RicercaService;
import it.orma.iridehr.services.helper.DocumentoAlfresco;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

@Controller
public class CVPreviewController  {
	/*
	@Autowired
	private AlfrescoService alfrescoService;
	
	@RequestMapping(value = "/downloadPDF", method = RequestMethod.GET)
	protected ModelAndView downloadPDF(@RequestParam("foo") String output) throws Exception {
		
		DocumentoAlfresco doc = alfrescoService.estraiTestoDalDocumento(output);
		
		return new ModelAndView("CVPreview","docAlfresco",doc);
	}
	*/
	@Autowired
	private RicercaRepository ricercaRepository;
	
	@Autowired
	private RicercaService ricercaService;
	
	@RequestMapping(value = "/downloadPDF", method = RequestMethod.GET)
	public ModelAndView showPDF(HttpServletRequest request, HttpServletResponse response,@RequestParam String printType) {
	    SchedaCandidato sc = (SchedaCandidato)request.getSession().getAttribute("resultScheda");
	    //model.addAttribute("resultScheda", result);
	    if(sc.getIdSchedaCandidato()==null)
			return null;
		HashMap<String, Object> model = new HashMap<String, Object>();
		
		model.put("dg", ricercaService.caricaDatiGenerali(sc,null,null));
		model.put("esperienzeLavorative", ricercaService.caricaEsperienzeLavorative(sc));
		model.put("studi", ricercaService.caricaStudiCompiuti(sc));
		model.put("lingue", ricercaRepository.caricaLingueConosciute(sc));
		model.put("informatica", ricercaRepository.caricaConoscenzaInformatica(sc));
		model.put("altro", ricercaRepository.caricaAltreInfo(sc));
		model.put("note", ricercaRepository.caricaNote(sc));
		model.put("logo", request.getSession().getServletContext().getRealPath("/images/logo_pdf.png"));
		
		
		if ("shortLogo".equals(printType)){
			model.put("logoOn", new Boolean(true));
			return new ModelAndView("CVPreviewShort", model);
		}else if ("shortNoLogo".equals(printType)){
			return new ModelAndView("CVPreviewShort", model);
		}else if ("long".equals(printType))
			return new ModelAndView("CVPreviewLong", model);
		else{
			model.put("domande", ricercaService.caricaDomandeRisposte());
			model.put("attitudiniForm", ricercaService.caricaAttitudini(sc));
			return new ModelAndView("CVPreviewAnalisi", model);
		}
	}
}
